//
//  GPIndexViewController.m
//  gupiao
//
//  Created by mac on 2019/8/14.
//  Copyright © 2019 mac. All rights reserved.
//

#import "GPIndexViewController.h"
#import "GPIndexHeaderView.h"
#import "GPIndexListCell.h"
#import "GPSearchViewController.h"
#import "GPDetailViewController.h"
#import "GPDetailModel.h"
#import "DGPUtil.h"

#import "HHYZXStockLocalModel.h"
#import "FTQLoginController.h"

#import "GPAccountViewController.h"
#import "HHYApplyPZVC.h"

@interface GPIndexViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) GPIndexHeaderView *headerView;
@property (weak, nonatomic) IBOutlet HMTableView *tableView;
@property (nonatomic,strong) NSArray *sourceArr;
@property (weak, nonatomic) IBOutlet UIView *customNavView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cutomNavHeight;
@property (nonatomic,strong) NSArray *zixuanArr;


//laji code
@property(nonatomic,strong)UIImageView *tianqi;

@property(nonatomic,strong)UILabel *time;

@property(nonatomic,strong)UILabel *week;

@property(nonatomic,strong)UIImageView *pic;

@property(nonatomic,strong)UILabel *content;
//
@end

@implementation GPIndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.cutomNavHeight.constant = (HM_NAVHEIGHT);
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"InfoData" ofType:@"plist"];
    NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    _sourceArr = [GPDetailModel mj_objectArrayWithKeyValuesArray:dataDic[@"data"]];;

    
    if (kUserDefaultObjectForKey(@"userid")==nil) {
        dispatch_async(dispatch_get_main_queue(), ^{
            FTQLoginController* login=[[FTQLoginController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
            [self presentViewController:nav animated:YES completion:nil];
        });
       
    }

    [self loadZixuanData];


}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.tableHeaderView = self.headerView;
}

-(void)loadNewData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView endRefreshs];
        [self loadZixuanData];
    });
}

-(void)loadZixuanData{
    [DGPUtil requestRandomStockModelWithSuccessCallback:^(NSArray * _Nonnull datas) {
        NSLog(@"%@",datas);
        self.zixuanArr = [HHYZXStockLocalModel mj_objectArrayWithKeyValuesArray:datas];
        for (int i = 0; i < 3; i ++) {
            HHYZXStockLocalModel *model = self.zixuanArr[i];
            float changepercent = [model.changepercent floatValue];
            UILabel *name = [self.headerView viewWithTag:i+1];
            UILabel *gains = [self.headerView viewWithTag:4+i];
            name.text = model.name;
            gains.text = [NSString stringWithFormat:@"%@%@%@",changepercent<0?@"":@"+",model.changepercent,@"%"];
        }
    } failedCallback:^{
        
    }];
}

- (IBAction)searchAction:(id)sender {
    GPSearchViewController *svc = [[GPSearchViewController alloc] init];
    svc.navTitle = @"搜索";
    svc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:svc animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _sourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GPDetailModel *model = self.sourceArr[indexPath.row];
    GPIndexListCell *cell = [GPIndexListCell cellWithTableView:tableView :indexPath];
    cell.img.image = [UIImage imageNamed:model.img];
    cell.title.text = model.title;
//    cell.date.text = [NSString stringWithFormat:@"%@ %@",model.date,model.from];
    cell.date.text = [NSString stringWithFormat:@"%@ ",model.date];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>self.cutomNavHeight.constant && self.customNavView.alpha == 0) {
        [UIView animateWithDuration:0.25 animations:^{
            self.customNavView.alpha = 1;
        }];
    }else if (scrollView.contentOffset.y<self.cutomNavHeight.constant && self.customNavView.alpha == 1){
        [UIView animateWithDuration:0.25 animations:^{
            self.customNavView.alpha = 0;
        }];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GPDetailViewController *dvc = [[GPDetailViewController alloc] init];
    dvc.navTitle = @"详情";
    dvc.model = self.sourceArr[indexPath.row];
    dvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:dvc animated:YES];
}

-(GPIndexHeaderView *)headerView{
    if (!_headerView) {
        __weak typeof(self) weakSelf = self;
        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"GPIndexHeaderView" owner:nil options:nil] lastObject];
        _headerView.sourceArr = @[@"banner 1",@"banner 2",@"banner 3"];
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, (240*FC_AUTO)+49+_headerView.stackView.height+20+170);
        _headerView.changeBlock = ^{
            [weakSelf loadZixuanData];
        };
        _headerView.addBlock = ^(UIButton * _Nonnull btn) {
            HHYZXStockLocalModel *model = weakSelf.zixuanArr[btn.tag-7];
            model.createTime = [[NSDate date] timeIntervalSince1970];
            model.userID = [DGPUtil getCurrentUserID];
            [DGPUtil addZiXuanModel:model successCallback:^{
                [MBProgressHUD showInformation:@"已添加到“自选”" toView:weakSelf.view andAfterDelay:HUD_TIME];
            } failedCallback:^{
                [MBProgressHUD showInformation:@"已添加到“自选”" toView:weakSelf.view andAfterDelay:HUD_TIME];
            }];
        };
        
        
        //立即申请 51
        _headerView.applyBlock = ^(UIButton * _Nonnull btn) {
//            GPAccountViewController *avc = [[GPAccountViewController alloc] init];
//            avc.navTitle = @"我的账户";
//            avc.hidesBottomBarWhenPushed = YES;
//            [weakSelf.navigationController pushViewController:avc animated:YES];
            
            HHYApplyPZVC *aVC = [[HHYApplyPZVC alloc] initWithType:(btn.tag-51)];
            aVC.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:aVC animated:YES];

            
        };
    }
    return _headerView;
}

-(void)setUp{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"GPIndexListCell" bundle:nil] forCellReuseIdentifier:@"GPIndexListCell"];
    [self.tableView addRefreshViews:self];
    self.tableView.mj_footer.hidden = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
